﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XNAStuding
{
    /// <summary>
    /// this class is used to dragwdrop the textures(tools)of experiemnt 3
    /// </summary>   
    class dragIncline : DrawableGameComponent 
    {
        Vector2 mouseposition = new Vector2();
        Rectangle posD;
        Rectangle posTex;
        drawRot org;
        drawRot inclinehead;
        bool mouseOver;//check if the mouse pressed over the object or not
        bool movable;//check if the object can moveo r not 
        /// <summary>
        /// Constractor of the class
        /// </summary>
        /// <param name="game"></param>
        /// <param name="org">object form class drawRot represent the Mini Picture of the incline</param>
        /// <param name="posD">the position of the distination rectangle that if the player released the mouse the object will be drawn</param>
        /// <param name="posTex">the position of the source rectangle from which the texture will be drew(Big One)</param>
        /// <param name="inclinehead">object form class drawRot represent the head of the incline</param>
        
        public dragIncline(Game game,drawRot org,Rectangle posD,Rectangle posTex,drawRot inclinehead): base(game)
        {
            this.inclinehead =inclinehead;
            this.org = org;
            this.posD = posD;
            movable = true;
            this.posTex = posTex;
        }
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
           
                base.Initialize();
                inclinehead.Visible = false;
                Globals.game.Components.Add(inclinehead);
           
        }
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            
                MouseState mouse_state = Mouse.GetState();
                mouseposition.X = mouse_state.X;
                mouseposition.Y = mouse_state.Y;
                Rectangle mouseRec = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 1, 1);
                //rectangele that contains the mouse 
                //if the two rectangels intersect each other 
                if (org.posD.Intersects(mouseRec) && mouse_state.LeftButton == ButtonState.Pressed && movable)
                {
                    mouseOver = true;
                }
                //if we pressed over the object 
                if (mouseOver)
                {
                    mouseOver = false;
                    //To Move the Object in the center of the mouse
                    org.posD.X = (int)mouseposition.X - (int)(129/2);
                    org.posD.Y = (int)mouseposition.Y - (int)(103/2);

                }
                //if we reached the right postion 
                if (org.posD.X >= posD.X && org.posD.X <= posD.X + 500 && org.posD.Y >= posD.Y && org.posD.Y <= posD.Y + 500 && mouse_state.LeftButton == ButtonState.Released)
                {
                    org.posTex = posTex;
                    org.posD.Width = posTex.Width;
                    org.posD.Height = posTex.Height;
                    //Right postion of the Incline
                    org.posD.X = posD.X + 40;
                    org.posD.Y = posD.Y + 300;
                   
                    if (Globals.step == 0)
                        Globals.step = 1;
                    movable = false;
                    inclinehead.Enabled = true;
                    inclinehead.Visible = true;       
                
            }
            base.Update(gameTime);
        }
        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Draw(GameTime gameTime)
        {       
           if (movable == false)
            inclinehead.Draw(gameTime);
           org.Draw(gameTime);
           base.Draw(gameTime);

        }
    }
}